<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: linear-gradient(135deg, #74ebd5, #acb6e5);
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        
        .container {
            width: 100%;
            max-width: 400px;
            padding: 20px;
        }
        
        .register-box {
            width: 500px;
            background: #fff;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
            padding: 30px;
            text-align: center;
        }
        
        .register-box h2 {
            color: #333;
            margin-bottom: 30px;
            font-size: 2.5rem;
        }
        
        .input-group {
            margin-bottom: 20px;
            text-align: left;
        }
        
        .input-group label {
            display: block;
            margin-bottom: 5px;
            color: #666;
            font-weight: 500;
        }
        
        .input-group input {
            width: 95%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 1rem;
            transition: border-color 0.3s ease;
        }
        
        .input-group input:focus {
            outline: none;
            border-color: #007bff;
            box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
        }
        
        button {
            width: 100%;
            padding: 12px;
            background: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            font-size: 1rem;
            font-weight: bold;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        
        button:hover {
            background: #0056b3;
        }
        
        .login-link {
            margin-top: 20px;
            font-size: 0.9rem;
            color: #666;
            text-align: center;
        }
        
        .login-link a {
            color: #007bff;
            text-decoration: none;
            font-weight: bold;
        }
        
        .login-link a:hover {
            text-decoration: underline;
        }
        
        .error-message {
            color: red;
            font-size: 0.9rem;
            margin-top: 5px;
        }
        
        .vercode {
            position: absolute;
            right: 69px;
            top: 25px;
            display: block;
            width: 100px;
            height: 50px;
            background-color: black;
            color: white;
            text-align: center;
            line-height: 50px;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="register-box">
            <h2>用户登录</h2>
            <form id="loginForm">
                <div class="input-group">
                    <label for="username">用户名</label>
                    <input type="text" id="username" name="username" required>
                    <span class="error-message" id="usernameError"></span>
                </div>
                <div class="input-group">
                    <label for="password">密码</label>
                    <input type="password" id="password" name="password" required placeholder="6~10个字符，含有大小写字母+数字">
                    <span class="error-message" id="passwordError"></span>
                </div>
                <div class="input-group" style="position: relative;">
                    <label for="vercode">验证码</label>
                    <input type="text" id="vercode" name="verCode" required style="width: 300px;">
                    <span class="vercode"></span>
                    <a href="#" style="padding-left: 110px;" onclick="gx(); return false;">换一个</a>
                </div>
                <button type="submit">登录</button>
                <p class="login-link">没有账号？ <a href="../注册/注册.html">注册</a></p>
            </form>
        </div>
    </div>

    <script src="https://cdn.staticfile.org/jquery/3.6.0/jquery.min.js"></script>
    <script>
        // 获取验证码
        function getVercode() {
            $.ajax({
                type: 'GET',
                url: 'http://47.94.4.201/index.php/index/index/getcode',
                dataType: 'json',
                success: function(data) {
                    $('.vercode').text(data.msg);
                },
                error: function(e) {
                    console.log(e);
                    alert('验证码获取失败，请重试');
                }
            });
        }


        getVercode();


        function gx() {
            getVercode();
        }


        $(document).ready(function() {
            $('#loginForm').submit(function(event) {
                event.preventDefault(); // 阻止表单默认提交行为

                // 获取表单数据
                const username = $('#username').val();
                const password = $('#password').val();
                const vercode = $('#vercode').val();

                // 清空错误信息
                $('.error-message').text('');

                // 密码格式验证
                if (!/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,10}$/.test(password)) {
                    $('#passwordError').text('*密码格式不正确');
                    return;
                }

                // 打印调试信息
                console.log('用户名:', username);
                console.log('密码:', password);
                console.log('验证码:', vercode);

                // 发送登录请求
                $.ajax({
                    type: 'POST',
                    url: 'http://47.94.4.201/index.php/index/index/login',
                    dataType: 'json',
                    data: {
                        name: username,
                        pwd: password,
                        vercode: vercode
                    },
                    success: function(data) {
                        console.log('服务器返回:', data); // 打印服务器返回的数据
                        if (data.msg === '登录成功') {
                            alert('登录成功！');
                            location.assign('../首页/首页.html'); // 跳转到首页
                        } else {
                            if (data.msg === '用户名或密码错误') {
                                $('#usernameError').text('*用户名或密码错误');
                            } else if (data.msg === '验证码错误') {
                                $('#vercodeError').text('*验证码错误');
                            } else {
                                alert('登录成功！');
                                location.assign('../首页/首页.html'); // 跳转到首页
                            }
                        }
                    },
                    error: function(e) {
                        console.log('请求失败:', e); // 打印错误信息
                        alert('请求失败，请稍后重试');
                    }
                });
            });
        });
    </script>
</body>

</html>